<template>
  <div class="table-page">
    <GiTable
      ref="tableRef"
      row-key="id"
      title="压测项目管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
        <a-input v-model="queryForm.version" placeholder="请输入乐观锁" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.createdBy" placeholder="请输入创建者" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['testing:project:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
        <a-tooltip content="导出">
          <a-button v-permission="['testing:project:export']" class="gi_hover_btn-border" @click="onExport">
            <template #icon>
              <icon-download />
            </template>
          </a-button>
        </a-tooltip>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['testing:project:update']" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['testing:project:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <ProjectAddModal ref="ProjectAddModalRef" @save-success="search" />
    <ProjectDetailDrawer ref="ProjectDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import ProjectAddModal from './ProjectAddModal.vue'
import ProjectDetailDrawer from './ProjectDetailDrawer.vue'
import { type ProjectResp, type ProjectQuery, deleteProject, exportProject, listProject } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'Project' })

const queryForm = reactive<ProjectQuery>({
  version: '',
  createdBy: '',
  sort: ['createdAt,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listProject({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: 'ID', dataIndex: 'id', slotName: 'id', width: 200 },
  { title: '项目管理员ID', dataIndex: 'projectAdmin', slotName: 'projectAdmin' },
  { title: '项目名称', dataIndex: 'name', slotName: 'name' },
  { title: '描述', dataIndex: 'description', slotName: 'description' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['testing:project:update', 'testing:project:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.version = ''
  queryForm.createdBy = ''
  search()
}

// 删除
const onDelete = (item: ProjectResp) => {
  return handleDelete(() => deleteProject(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportProject(queryForm))
}

const ProjectAddModalRef = ref<InstanceType<typeof ProjectAddModal>>()
// 新增
const onAdd = () => {
  ProjectAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: ProjectResp) => {
  ProjectAddModalRef.value?.onUpdate(item.id)
}

const ProjectDetailDrawerRef = ref<InstanceType<typeof ProjectDetailDrawer>>()
// 详情
const onDetail = (item: ProjectResp) => {
  ProjectDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
